// Main script for module 'KeyboardMovement'

//****************************************************************************************************
// DEFINITIONS
//****************************************************************************************************

#define DISTANCE 10000// distance player walks in Tapping mode before he stops

enum KeyboardMovement_Directions {
	eKeyboardMovement_Stop, 
	eKeyboardMovement_DownLeft, 
	eKeyboardMovement_Down, 
	eKeyboardMovement_DownRight, 
	eKeyboardMovement_Left, 
	eKeyboardMovement_Right, 
	eKeyboardMovement_UpLeft, 
	eKeyboardMovement_Up, 
	eKeyboardMovement_UpRight
};

//****************************************************************************************************
// VARIABLES
//****************************************************************************************************

// keycodes as variables for future key customization functions (static variables?):
int KeyboardMovement_KeyDown = 380; // down arrow
int KeyboardMovement_KeyLeft = 375; // left arrow
int KeyboardMovement_KeyRight = 377; // right arrow
int KeyboardMovement_KeyUp = 372; // up arrow
int KeyboardMovement_KeyDownRight = 381; // PgDn (numpad)
int KeyboardMovement_KeyUpRight = 373; // PgUp (numpad)
int KeyboardMovement_KeyDownLeft = 379; // End (numpad)
int KeyboardMovement_KeyUpLeft = 371; // Home (numpad)
int KeyboardMovement_KeyStop = 376; // 5 (numpad)

KeyboardMovement_Modes KeyboardMovement_Mode = eKeyboardMovement_None; // stores current keyboard control mode (disabled by default)

KeyboardMovement_Directions KeyboardMovement_CurrentDirection = eKeyboardMovement_Stop; // stores current walking direction of player character

//****************************************************************************************************
// USER FUNCTIONS
//****************************************************************************************************

//====================================================================================================

static function KeyboardMovement::SetMode(KeyboardMovement_Modes mode) {
	KeyboardMovement_Mode = mode;
}

//====================================================================================================

// key customization functions here

//====================================================================================================

//****************************************************************************************************
// EVENT HANDLER FUNCTIONS
//****************************************************************************************************

//====================================================================================================

function repeatedly_execute() {

	//--------------------------------------------------
	// Pressing mode
	//--------------------------------------------------

	if ((IsGamePaused() == true) || (KeyboardMovement_Mode != eKeyboardMovement_Pressing) || (IsInterfaceEnabled() == false) || (player.on == false)) return 0;
	  // if game is paused, module or mode disabled, interface disabled or player character hidden, quit function

	KeyboardMovement_Directions newdirection; // declare variable storing new direction

	// get new direction:
	if ( ((IsKeyPressed(KeyboardMovement_KeyDown)) && (IsKeyPressed(KeyboardMovement_KeyRight))) || (IsKeyPressed(KeyboardMovement_KeyDownRight)) ) newdirection = eKeyboardMovement_DownRight; // if down&right arrows or PgDn (numeric pad) held down, set new direction to Down-Right
	else if ( ((IsKeyPressed(KeyboardMovement_KeyUp)) && (IsKeyPressed(KeyboardMovement_KeyRight))) || (IsKeyPressed(KeyboardMovement_KeyUpRight)) ) newdirection = eKeyboardMovement_UpRight; // up&right arrows or PgUp (numpad)
	else if ( ((IsKeyPressed(KeyboardMovement_KeyDown)) && (IsKeyPressed(KeyboardMovement_KeyLeft))) || (IsKeyPressed(KeyboardMovement_KeyDownLeft)) ) newdirection = eKeyboardMovement_DownLeft; // down&left arrows or End (numpad)
	else if ( ((IsKeyPressed(KeyboardMovement_KeyUp)) && (IsKeyPressed(KeyboardMovement_KeyLeft))) || (IsKeyPressed(KeyboardMovement_KeyUpLeft)) ) newdirection = eKeyboardMovement_UpLeft; // up&left arrows or Home (numpad)
	else if (IsKeyPressed(KeyboardMovement_KeyDown)) newdirection = eKeyboardMovement_Down; // down arrow
	else if (IsKeyPressed(KeyboardMovement_KeyLeft)) newdirection = eKeyboardMovement_Left; // left arrow
	else if (IsKeyPressed(KeyboardMovement_KeyRight)) newdirection = eKeyboardMovement_Right; // right arrow
	else if (IsKeyPressed(KeyboardMovement_KeyUp)) newdirection = eKeyboardMovement_Up; // up arrow
	else newdirection = eKeyboardMovement_Stop; // if none of the above held down, set it to stop player character

	if (IsKeyPressed(KeyboardMovement_KeyStop)) newdirection = eKeyboardMovement_Stop; // if 5 (numeric pad) held down, stop player character, regardless of whether some of the above are held down

	if (newdirection != KeyboardMovement_CurrentDirection) { // if new direction is different from current direction

		if (newdirection == eKeyboardMovement_Stop) player.StopMoving(); // if new direction is the Stop command, stop movement of player character
		else { // if new direction is NOT the Stop command

			int dx, dy; // declare variables storing new walk coordinates
			if (newdirection == eKeyboardMovement_DownRight) {
				dx = DISTANCE;
				dy = DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_UpRight) {
				dx = DISTANCE;
				dy = -DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_DownLeft) {
				dx = -DISTANCE;
				dy = DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_UpLeft) {
				dx = -DISTANCE;
				dy = -DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_Down) {
				dx = 0;
				dy = DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_Left) {
				dx = -DISTANCE;
				dy = 0;
			}
			else if (newdirection == eKeyboardMovement_Right) {
				dx = DISTANCE;
				dy = 0;
			}
			else if (newdirection == eKeyboardMovement_Up) {
				dx = 0;
				dy = -DISTANCE;
			}

			player.WalkStraight(player.x + dx, player.y + dy, eNoBlock); // walk player character to the new coordinates
		}
		KeyboardMovement_CurrentDirection = newdirection; // update current direction to new direction

	}

}

//====================================================================================================

function on_key_press(int keycode) {

	//--------------------------------------------------
	// Tapping mode
	//--------------------------------------------------

	if ((IsGamePaused() == true) || (KeyboardMovement_Mode != eKeyboardMovement_Tapping) || (IsInterfaceEnabled() == false) || (player.on == false)) return 0;
	  // if game is paused, module or mode disabled, interface disabled or player character hidden, quit function

	KeyboardMovement_Directions newdirection; // declare variable storing new direction

	// get new direction:
	if (keycode == KeyboardMovement_KeyDownRight) newdirection = eKeyboardMovement_DownRight; // if down-right key pressed, set new direction to Down-Right
	else if (keycode == KeyboardMovement_KeyUpRight) newdirection = eKeyboardMovement_UpRight;
	else if (keycode == KeyboardMovement_KeyDownLeft) newdirection = eKeyboardMovement_DownLeft;
	else if (keycode == KeyboardMovement_KeyUpLeft) newdirection = eKeyboardMovement_UpLeft;
	else if (keycode == KeyboardMovement_KeyDown) newdirection = eKeyboardMovement_Down;
	else if (keycode == KeyboardMovement_KeyLeft) newdirection = eKeyboardMovement_Left;
	else if (keycode == KeyboardMovement_KeyRight) newdirection = eKeyboardMovement_Right;
	else if (keycode == KeyboardMovement_KeyUp) newdirection = eKeyboardMovement_Up;
	else if (keycode == KeyboardMovement_KeyStop) newdirection = eKeyboardMovement_Stop; // if stop key pressed, set to stop player character

	if (newdirection != KeyboardMovement_CurrentDirection) { // if new direction is different from current direction

		if (newdirection == eKeyboardMovement_Stop) player.StopMoving(); // if new direction is the Stop command, stop movement of player character
		else { // if new direction is NOT the Stop command

			int dx, dy; // declare variables storing new walk coordinates
			if (newdirection == eKeyboardMovement_DownRight) {
				dx = DISTANCE;
				dy = DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_UpRight) {
				dx = DISTANCE;
				dy = -DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_DownLeft) {
				dx = -DISTANCE;
				dy = DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_UpLeft) {
				dx = -DISTANCE;
				dy = -DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_Down) {
				dx = 0;
				dy = DISTANCE;
			}
			else if (newdirection == eKeyboardMovement_Left) {
				dx = -DISTANCE;
				dy = 0;
			}
			else if (newdirection == eKeyboardMovement_Right) {
				dx = DISTANCE;
				dy = 0;
			}
			else if (newdirection == eKeyboardMovement_Up) {
				dx = 0;
				dy = -DISTANCE;
			}

			player.WalkStraight(player.x + dx, player.y + dy, eNoBlock); // walk player character to the new coordinates
		}
		KeyboardMovement_CurrentDirection = newdirection; // update current direction to new direction

	}
	else { // if new direction is same as current direction
		player.StopMoving(); // stop player character
		KeyboardMovement_CurrentDirection = eKeyboardMovement_Stop; // update current direction
	}

}

//====================================================================================================

function on_event(EventType event, int data) {

	if (event == eEventLeaveRoom) KeyboardMovement_CurrentDirection = eKeyboardMovement_Stop;

}

//====================================================================================================
